//
//  BaseViewController.m
//  mentor
//
//  Created by 孙宏峰的MacPro on 2018/7/3.
//  Copyright © 2018年 孙宏峰的MacPro. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController ()
@property (nonatomic,strong)UIButton *userHeadBtn;
@property (nonatomic,strong)UIView *leftBarItemView;


@end

@implementation BaseViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.userNameLabel.text = [CurrentUser shared].showUserName;
    self.userHeadImage.image = [[CurrentUser shared].xingb isEqualToString:@"男"] ? [UIImage imageNamed:@"man"] : [UIImage imageNamed:@"woman"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc]initWithCustomView:self.leftBarItemView];
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
        self.navigationItem.rightBarButtonItem = [self getBarButtonItemWithTitle:@"注销" withAction:@selector(zhux) withColor:[UIColor blackColor]];
}
//通过文字自定义BarButtonItem
- (UIBarButtonItem *)getBarButtonItemWithTitle:(NSString *)string withAction:(SEL)action withColor:(UIColor *)color
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0,  70, 30);
    
    [btn setTitleEdgeInsets:(UIEdgeInsetsMake(0, 0, 0, 0))];
    [btn setTitle:string forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:color forState:UIControlStateNormal];
    UIBarButtonItem *bit = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    return bit;
}
- (void)zhux
{
  
}
//- (void)viewDidAppear:(BOOL)animated {
//    [super viewDidAppear:animated];
//    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
//    }
//}
//
//- (void)viewDidDisappear:(BOOL)animated {
//    [super viewDidDisappear:animated];
//    //在其他离开改页面的方法同样加上下面代码
//    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
//        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
//    }
//}

#pragma mark - getters and setters
/*!@brief ****   用户头像 */
-(UIImageView *)userHeadImage
{
    if(!_userHeadImage) {
        _userHeadImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, 36, 36)];
        // 设置用户头像圆角
        _userHeadImage.layer.masksToBounds = YES;
        _userHeadImage.layer.cornerRadius = 18;
        [_userHeadImage setImage:[UIImage imageNamed:@"user_de"]];
    }
    return _userHeadImage;
}
/*!@brief ****   自定义导航左侧按钮 */
-(UIView *)leftBarItemView
{
    if(!_leftBarItemView){
        
        // 创建底层View
        //首页添加搜索,so缩短头像父容器宽度
        _leftBarItemView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        
        // 将用户头像加入底层View
        [_leftBarItemView addSubview:self.userHeadImage];
        // 将用户名称加入地城View
        [_leftBarItemView addSubview:self.userNameLabel];
        
        
        [_leftBarItemView addSubview:self.userHeadBtn];
        
    }
    return _leftBarItemView;
}
/*!@brief ****   用户姓名label标签 */
-(UILabel *)userNameLabel
{
    if(!_userNameLabel) {
        // 获取用户信息
        //        ModelUser *user = [DBManager searchWithId:[HDUserDefaults objectForKey:KUD_USERID]];
        _userNameLabel = [[UILabel alloc] init];
        // 创建用户名称
        _userNameLabel.font = [UIFont systemFontOfSize:16];
        
        _userNameLabel.text = @"用户名";
        
        [_userNameLabel sizeToFit];
        _userNameLabel.left = self.userHeadImage.right + 5;
        _userNameLabel.textColor = [UIColor blackColor];
        _userNameLabel.centerY = self.userHeadImage.centerY;
        
        //        _userNameLabel.hidden = YES;
        
    }
    return _userNameLabel;
}
/*!@brief ****   用户头像btn */
-(UIButton *)userHeadBtn
{
    if(!_userHeadBtn){
        _userHeadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _userHeadBtn.frame = CGRectMake(self.userHeadImage.origin.x, self.userHeadImage.origin.y,self.userNameLabel.origin.x + self.userNameLabel.size.width,44);
        [_userHeadBtn addTarget:self action:@selector(userCenter) forControlEvents:UIControlEventTouchUpInside];
    }
    return _userHeadBtn;
}
/*!@brief ****   点击用户头像 */
- (void)userCenter
{
    
}

@end
